﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using VIA.SiMoJi.Domain.Option;

namespace VIA.Project.Presentation.View.Inspection
{
    /// <summary>
    /// TriggerOptionView.xaml 的交互逻辑
    /// </summary>
    public partial class TriggerOptionView : UserControl
    {
        #region 依赖属性
        public ObservableCollection<TriggerOption> TriggerOptions
        {
            get { return (ObservableCollection<TriggerOption>)GetValue(TriggerOptionsProperty); }
            set { SetValue(TriggerOptionsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TriggerOptionsProperty =
            DependencyProperty.Register("TriggerOptions", typeof(ObservableCollection<TriggerOption>), typeof(TriggerOptionView), new PropertyMetadata(new ObservableCollection<TriggerOption>()));

        #endregion

        #region 路由事件
        // Register a custom routed event using the Bubble routing strategy.
        public static readonly RoutedEvent TriggerEvent = EventManager.RegisterRoutedEvent(
            name: "Triggered",
            routingStrategy: RoutingStrategy.Bubble,
            handlerType: typeof(RoutedEventHandler),
            ownerType: typeof(TriggerOptionView));

        // Provide CLR accessors for assigning an event handler.
        public event RoutedEventHandler Triggered
        {
            add { AddHandler(TriggerEvent, value); }
            remove { RemoveHandler(TriggerEvent, value); }
        }

        void RaiseCustomRoutedEvent(TriggerOption triggerOption)
        {
            // Create a RoutedEventArgs instance.
            TriggerRoutedEventArgs routedEventArgs = new(triggerOption, routedEvent: TriggerEvent);

            // Raise the event, which will bubble up through the element tree.
            RaiseEvent(routedEventArgs);
        }

        #endregion

        public TriggerOptionView()
        {
            InitializeComponent();
            SetValue(TriggerOptionsProperty, new ObservableCollection<TriggerOption>());
        }

        private void Button_Add_Click(object sender, RoutedEventArgs e)
        {
            TriggerOptions.Add(new TriggerOption());
        }

        private void Button_Remove_Click(object sender, RoutedEventArgs e)
        {
            TriggerOption triggerOption = ListBox1.SelectedItem as TriggerOption;
            if (triggerOption != null)
            {
                TriggerOptions.Remove(triggerOption);
            }
        }

        private void Button_Trigger_Click(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;
            TriggerOption triggerOption = bt.DataContext as TriggerOption;
            RaiseCustomRoutedEvent(triggerOption);
        }

        private void ListBox1_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
            eventArg.RoutedEvent = UIElement.MouseWheelEvent;
            eventArg.Source = sender;
            ListBox1.RaiseEvent(eventArg);
        }
    }

    public class TriggerRoutedEventArgs : RoutedEventArgs
    {
        public TriggerOption TriggerOption { get; set; }

        public TriggerRoutedEventArgs(TriggerOption triggerOption, RoutedEvent routedEvent) : base(routedEvent)
        {
            TriggerOption = triggerOption;
        }

    }
}
